package com.example.administrator.commonlibrary.definedview.inheritView;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.commonlibrary.R;
import com.example.administrator.commonlibrary.R2;
import com.example.administrator.commonlibrary.utils.system.AppManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author wang_hong_wei
 * @description 页面标题
 * @Date 2018-5-11
 */

public class TitleLayout extends LinearLayout {


    @BindView(R2.id.rl_title_back)
    LinearLayout rlTitleBack;
    @BindView(R2.id.tv_title)
    AppCompatTextView tvTitle;
    @BindView(R2.id.iv_add)
    AppCompatImageView ivAdd;
    @BindView(R2.id.registered)
    AppCompatTextView registered;
    @BindView(R2.id.rigth_imag)
    AppCompatImageView rigthImag;
    @BindView(R2.id.ll_right_btn_group)
    LinearLayout llRightBtnGroup;



    private OnClickListener backOnClickListener;
    private OnClickListener rightOnClickListener;

    public TitleLayout(final Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.common_title, this);
        ButterKnife.bind(this, view);
        rlTitleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity((Activity) getContext());
            }
        });

    }

    /**
      * @desc  设置标题
      * @param
      */
    public void setTitle(String name) {
        this.tvTitle.setText(name);
    }

    public void setTitleImage(boolean flag, int img) {
        tvTitle.setVisibility(GONE);
        ivAdd.setVisibility(VISIBLE);
        this.ivAdd.setImageResource(img);
    }

    public String getTitle() {
        return tvTitle.getText().toString();
    }



/**
  * @desc 设置右边标题
  * @param
  */
    public void setRightTitle(String name) {
        this.registered.setVisibility(VISIBLE);
        this.registered.setText(name);
    }

    public void setRightImage(int imag) {
        rigthImag.setVisibility(VISIBLE);
        this.rigthImag.setImageResource(imag);
    }

    public String getRightTitle() {
        return registered.getText().toString();
    }

/**
  * @desc 设置返回按钮不可见
  * @param
  */
    public void setBackEnable(boolean flag) {
        if (!flag) {
            this.rlTitleBack.setVisibility(GONE);
        }
    }



    public void setBackOnclickListener(OnClickListener onclickListener) {
        this.backOnClickListener = onclickListener;
    }

    public void setRightOnClickListener(OnClickListener onclickListener) {
        this.rightOnClickListener = onclickListener;
    }



    @OnClick({R2.id.rl_title_back, R2.id.ll_right_btn_group})
    public void onClick(View view) {
        if (R2.id.rl_title_back==view.getId()){
            if (backOnClickListener != null) backOnClickListener.onClick(view);
        }else if (R2.id.ll_right_btn_group==view.getId()){
            if (rightOnClickListener != null) rightOnClickListener.onClick(view);
        }
    }


}

